package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.DiscountBean;
import com.project.mapper.IDiscountMapper;
import com.project.service.IDiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 促销业务类
 */
@Service
@Transactional
public class DiscountServiceImpl implements IDiscountService {
    @Autowired
    private IDiscountMapper discountMapper;
    @Override
    public String addDiscountProduct(DiscountBean discountObj) {
        if (discountObj !=null) {//对象不为空时，添加
            discountMapper.insert(discountObj);
            return "ok";
        }
        return "no";
    }

    @Override
    public IPage<DiscountBean> cutDiscountByItem(int page) {

        return discountMapper.selectPage(new Page<>(page,3),null);
    }

    @Override
    public String updatePromotionById(String shopId, String promotion) {

        if (!promotion.equals("未促销") &&!promotion.equals("促销")){//判断促销状态是否正确
            return "促销状态输入不符要求";
        }
        //通过编号查询
        QueryWrapper<DiscountBean> qw=new QueryWrapper<>();
        qw.eq("shopId",shopId);
        DiscountBean discountBean=discountMapper.selectOne(qw);
        if (discountBean == null){//判断对象是否为空
            return "编号输入有误";
        }
        //修改状态
//        discountBean.setPromotion(promotion);
        discountMapper.updateById(discountBean);
        return "ok";
    }

    @Override
    public String updateDiscountById(long id, double discount) {
        if (id <1){//判断id是否输入
            return "编号输入有误";
        }
        if (discount<0 ||discount>1){
            return "折扣输入不符要求";
        }
        //通过编号查询
        DiscountBean discountBean=discountMapper.selectById(id);
        if (discountBean == null){//判断对象是否为空
            return "编号输入有误";
        }
        //修改折扣
        discountBean.setDiscount(discount);
        discountMapper.updateById(discountBean);
        return "ok";
    }

    @Override
    public DiscountBean findByShopNumber(String shopId) {
        QueryWrapper<DiscountBean> qw=new QueryWrapper<>();
        qw.eq("shopId",shopId);
        return discountMapper.selectOne(qw);
    }
}
